import 'package:city_pickers/city_pickers.dart';
import 'package:city_pickers/modal/result.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_switch/flutter_switch.dart';
import 'package:shenfuhui/config/config.dart';

import '../routers/routers.dart';
import '../views/dialog_manager.dart';

///
/// 添加/修改收货地址
///
class MineAddressEdit extends StatefulWidget {
  const MineAddressEdit({Key? key}) : super(key: key);

  @override
  State<MineAddressEdit> createState() => _MineAddressEditState();
}

class _MineAddressEditState extends State<MineAddressEdit> {
  var _name = '';
  var _phone = '';
  var _area = '点击选择省市区';
  var _address = '';
  bool _isCheck = false;

  ///
  /// 弹出省市区三级选择器
  ///
  getCityResult() async {
    // type 1
    Result? result = await CityPickers.showCityPicker(
      context: context,
      height: 600.w,
      barrierOpacity: 0.5,
      theme: Theme.of(context).copyWith(),
    );
    if (result != null) {
      String str =
          '${result.provinceName}-${result.cityName}-${result.areaName}';
      setState(() {
        _area = str;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Config.colorBg2,
        appBar: AppBar(
          title: const Text('添加收货地址'),
          centerTitle: true,
        ),
        body: SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: Container(
            width: double.infinity,
            height: double.infinity,
            margin: EdgeInsets.symmetric(vertical: 25.w, horizontal: 25.w),
            child: Column(
              children: [
                Container(
                  width: double.infinity,
                  padding:
                      EdgeInsets.symmetric(vertical: 20.w, horizontal: 20.w),
                  decoration: BoxDecoration(
                    color: (Config.colorWhite),
                    borderRadius: BorderRadius.circular(10.w),
                  ),
                  child: Column(
                    children: [
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Container(
                            margin: EdgeInsets.symmetric(vertical: 5.w),
                            width: 100.w,
                            child: Text(
                              "收货人",
                              style: TextStyle(fontSize: 25.sp),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(left: 30.w),
                          ),
                          Container(
                            width: 520.w,
                            margin: EdgeInsets.symmetric(vertical: 5.w),
                            child: TextField(
                              keyboardType: TextInputType.text,
                              textAlign: TextAlign.justify,
                              obscureText: false,
                              maxLength: 20,
                              maxLines: 1,
                              onChanged: (value) {
                                setState(() {
                                  _name = value;
                                });
                              },
                              style: TextStyle(
                                  fontSize: 25.sp, color: Config.colorText1),
                              decoration: const InputDecoration(
                                hintStyle: TextStyle(color: Config.colorText3),
                                hintText: "请输入收货人姓名",
                                counterText: '',
                                isDense: true,
                                border: InputBorder.none,
                                contentPadding:
                                    EdgeInsets.zero, //设置文本内容的Padding
                              ),
                            ),
                          ),
                        ],
                      ),
                      Divider(
                        color: Config.colorDivider,
                        height: 40.w,
                        thickness: 2.w,
                      ),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Container(
                            margin: EdgeInsets.symmetric(vertical: 5.w),
                            width: 100.w,
                            child: Text(
                              "联系电话",
                              style: TextStyle(fontSize: 25.sp),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(left: 30.w),
                          ),
                          Container(
                            width: 520.w,
                            margin: EdgeInsets.symmetric(vertical: 5.w),
                            child: TextField(
                              keyboardType: TextInputType.phone,
                              textAlign: TextAlign.justify,
                              obscureText: false,
                              maxLength: 11,
                              maxLines: 1,
                              onChanged: (value) {
                                setState(() {
                                  _phone = value;
                                });
                              },
                              style: TextStyle(
                                  fontSize: 25.sp, color: Config.colorText1),
                              decoration: const InputDecoration(
                                hintStyle: TextStyle(color: Config.colorText3),
                                hintText: "请输入收货人联系电话",
                                counterText: '',
                                isDense: true,
                                border: InputBorder.none,
                                contentPadding:
                                    EdgeInsets.zero, //设置文本内容的Padding
                              ),
                            ),
                          ),
                        ],
                      ),
                      Divider(
                        color: Config.colorDivider,
                        height: 40.w,
                        thickness: 2.w,
                      ),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Container(
                            margin: EdgeInsets.symmetric(vertical: 5.w),
                            width: 100.w,
                            child: Text(
                              "所在地区",
                              style: TextStyle(fontSize: 25.sp),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(left: 30.w),
                          ),
                          InkWell(
                            onTap: () {
                              getCityResult();
                            },
                            child: Container(
                              width: 490.w,
                              margin: EdgeInsets.symmetric(vertical: 5.w),
                              child: Text(
                                _area,
                                style: TextStyle(fontSize: 25.sp),
                              ),
                            ),
                          ),
                          Icon(
                            Icons.arrow_forward_ios,
                            size: 25.w,
                          ),
                        ],
                      ),
                      Divider(
                        color: Config.colorDivider,
                        height: 40.w,
                        thickness: 2.w,
                      ),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Container(
                            margin: EdgeInsets.symmetric(vertical: 5.w),
                            width: 100.w,
                            child: Text(
                              "详细地址",
                              style: TextStyle(fontSize: 25.sp),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(left: 30.w),
                          ),
                          Container(
                            width: 520.w,
                            margin: EdgeInsets.symmetric(vertical: 5.w),
                            child: TextField(
                              keyboardType: TextInputType.text,
                              textAlign: TextAlign.justify,
                              obscureText: false,
                              maxLength: 100,
                              maxLines: 1,
                              onChanged: (value) {
                                setState(() {
                                  _phone = value;
                                });
                              },
                              style: TextStyle(
                                  fontSize: 25.sp, color: Config.colorText1),
                              decoration: const InputDecoration(
                                hintStyle: TextStyle(color: Config.colorText3),
                                hintText: "请输入详细地址",
                                counterText: '',
                                isDense: true,
                                border: InputBorder.none,
                                contentPadding:
                                    EdgeInsets.zero, //设置文本内容的Padding
                              ),
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                Container(
                  width: double.infinity,
                  margin: EdgeInsets.only(top: 25.w),
                  padding:
                      EdgeInsets.symmetric(vertical: 20.w, horizontal: 20.w),
                  decoration: BoxDecoration(
                    color: (Config.colorWhite),
                    borderRadius: BorderRadius.circular(10.w),
                  ),
                  child: Column(
                    children: [
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            margin: EdgeInsets.symmetric(vertical: 5.w),
                            child: Text(
                              "设为默认地址",
                              style: TextStyle(fontSize: 25.sp),
                            ),
                          ),
                          FlutterSwitch(
                            width: 96.w,
                            height: 44.w,
                            activeColor: Config.colorMain,
                            valueFontSize: 15.sp,
                            toggleSize: 38.w,
                            value: _isCheck,
                            borderRadius: 22.w,
                            padding: 5.w,
                            showOnOff: true,
                            onToggle: (val) {
                              setState(() {
                                _isCheck = val;
                              });
                            },
                          ),
                        ],
                      ),
                      Visibility(
                        visible: true,
                        child: Divider(
                          color: Config.colorDivider,
                          height: 40.w,
                          thickness: 2.w,
                        ),
                      ),
                      Visibility(
                        visible: true,
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Container(
                              margin: EdgeInsets.symmetric(vertical: 5.w),
                              child: Text(
                                "删除地址",
                                style: TextStyle(
                                    fontSize: 25.sp, color: Colors.red),
                              ),
                            ),
                            Container(),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  width: 750.w,
                  height: 100.w,
                  padding: EdgeInsets.symmetric(vertical: 8.w, horizontal: 8.w),
                  margin: EdgeInsets.only(top: 45.w),
                  child: ElevatedButton(
                    clipBehavior: Clip.antiAlias,
                    onPressed: () => {
                      Routes.router.navigateTo(context, Routes.mineAddressEdit,
                          transition: TransitionType.inFromRight)
                    },
                    style:
                        ElevatedButton.styleFrom(shape: const StadiumBorder()),
                    child: const Text('保存'),
                  ),
                ),
              ],
            ),
          ),
        ));
  }
}
